package com.lipuwater.agent.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.view.View;

import com.ismartlib.utils.NetUtils;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.adapter.BaseListAdapter;
import com.lipuwater.agent.app.base.ui.BaseListActivity;
import com.lipuwater.agent.app.base.utils.ListSettings;
import com.lipuwater.agent.http.ApiCallBack;
import com.lipuwater.agent.http.HttpConfig;
import com.lipuwater.agent.http.ResponseParams;
import com.lipuwater.agent.http.api.ApiRequestsBasis;
import com.lipuwater.agent.http.bean.user.PersonnelInfoBean;
import com.lipuwater.agent.http.bean.user.PersonnelListBean;
import com.lipuwater.agent.ui.adapter.ATPersonnelAdapter;
import com.lipuwater.agent.utils.MapToBeanUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2017/2/20.
 * Email:924686754@qq.com
 */
public class PersonnelListActivity extends BaseListActivity<PersonnelInfoBean> {
    private int PAGE_INDEX = 1;
    private int PAGE_SIZE = 15;
    private boolean isShowLoading = true;

    @Override
    protected void OnViewCreated() {
        toolbar.setBackgroundResource(R.drawable.img_title_bar);
        if (getIntent().getBooleanExtra("choose", false)) {
            initTitle(R.drawable.icon_aleft_white, "工单分配");
        } else {
            initTitle(R.drawable.icon_aleft_white, "人员管理");
        }
        toolbarTitle.setTextColor(Color.parseColor("#ffffff"));
        showLoading();
        toAdd();
    }

    @Override
    public void requestData(boolean needRefresh) {
        if (needRefresh) {
            PAGE_INDEX = 1;
        }
        if (!isShowLoading) {
            isShowLoading = true;
            showLoading();
        }
        getPersonnelList();
    }

    @Override
    protected BaseListAdapter<PersonnelInfoBean> getListAdapter() {
        return new ATPersonnelAdapter(this, getIntent().getBooleanExtra("choose", false), getIntent().getStringExtra("chooseType"));
    }

    @Override
    protected ListSettings getBaseSettings() {
        ListSettings settings = new ListSettings();
        settings.setCustomLayoutId(R.layout.activity_personnel);
        return settings;
    }

    private void getPersonnelList() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", token);
        params.put("pageIndex", PAGE_INDEX);
        params.put("pageSize", PAGE_SIZE);
        ApiRequestsBasis.getPersonnelList(this, params, new ApiCallBack() {
            @Override
            public void onResponse(ResponseParams result) {
                if (result.getStatus() == HttpConfig.success) {
                    setData(result);
                    if (isShowLoading) {
                        hideEmptyLayout();
                    }
                } else {
                    if (isShowLoading) {
                        isShowLoading = false;
                        showNoNetWork();
                    }
                    Toast(result.getMessage());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                if (isShowLoading) {
                    isShowLoading = false;
                    showNoNetWork();
                }
                onDataLoaded(null);//没网络记得调用onDataLoaded，否则导致刷新被禁止
                if (!NetUtils.isConnected(PersonnelListActivity.this)) {
                    Toast(getString(R.string.network_fails));
                } else {
                    Toast(getString(R.string.service_error));
                }
            }
        });
    }

    private void setData(ResponseParams result) {
        if (PAGE_INDEX == 1) {
            mAdapter.clear();
        }
        PersonnelListBean personnelList = (PersonnelListBean) MapToBeanUtil.toJavaBean(result.getParams(), PersonnelListBean.class);
        if (personnelList != null && personnelList.getList().size() > 0) {
            PAGE_INDEX++;
            onDataLoaded(personnelList.getList());
        } else {
            onDataLoaded(null);
        }
    }

    private void toAdd() {
        FloatingActionButton btnAdd = (FloatingActionButton) mViewBuilder.mContentView.findViewById(R.id.fab);
        if (getIntent().getBooleanExtra("choose", false)) {
            btnAdd.setVisibility(View.GONE);
            return;
        }
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PersonnelListActivity.this, PersonnelEditOrAdd.class);
                intent.putExtra("func", "add");
                startActivityForResult(intent, 1);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && resultCode == 101) {
            boolean update = data.getBooleanExtra("update", false);
            if (update) {
                PAGE_INDEX = 1;
                getPersonnelList();
            }
        }
    }
}
